package cn.edu.scau.cmi.luotiansheng.hibernate.service.Impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.edu.scau.cmi.luotiansheng.hibernate.domain.Student;
import cn.edu.scau.cmi.luotiansheng.hibernate.domain.Teacher;
import cn.edu.scau.cmi.luotiansheng.hibernate.service.StudentService;
import cn.edu.scau.cmi.luotiansheng.hibernate.util.HibernateUtil;



public class StudentServiceImpl implements StudentService {
	
	private Session session=HibernateUtil.getSession();

	@Override
	public Student getStu(Long id) {
		// TODO Auto-generated method stub
		session=HibernateUtil.getSession();
		Transaction tx=session.getTransaction();
		tx.begin();
		Student t=session.get(Student.class, id);
		tx.commit();
		
		return t;
	}

	@Override
	public void delStd(Student student) {
		// TODO Auto-generated method stub
		session=HibernateUtil.getSession();
		Transaction tx=session.getTransaction();
		tx.begin();
		session.delete(student);
		tx.commit();

		
	}

	@Override
	public void saveStu(Student student) {
		// TODO Auto-generated method stub
		session=HibernateUtil.getSession();
		Transaction tx=session.getTransaction();
		tx.begin();
		session.save(student);
		tx.commit();
	}
	
	
	@Override
	public void updateStu(Student student) {
		// TODO Auto-generated method stub
		session=HibernateUtil.getSession();
		Transaction tx=session.getTransaction();
		tx.begin();
		/*
		 * Student student2=session.get(Student.class,student.getId() );
		 * student2.setName(student.getName()); stud
		 */
		
		session.update(student);
		tx.commit();
	}
	

	@Override
	public List<Student> getAllStu() {
		// TODO Auto-generated method stub
		session=HibernateUtil.getSession();
		Transaction tx=session.getTransaction();
		tx.begin();
		
		List<Student> list=session.createSQLQuery("select * from student").addEntity(Student.class).list();
		
		tx.commit();
		
		return list;
	}

	



}
